package controllers;

import models.User;
import play.data.validation.Required;
import play.libs.Crypto;
import play.libs.Mail;
import play.mvc.Controller;
import play.mvc.With;


public class Application extends Controller {
	//renders the first page inorder to make the user register or login
    public static void index() {
    	if(!Registration.registered) render("Application/firstPage.html");
    	
    	else {
    		Admin.setConnectedUser();
    	}
    }
    public static void about() {
    	render();
    }
    
    public static void forgetPassword(String email)
	{
		
		User user = User.find("byEmail", email).first();
		
		if(user != null){
		Registration.registered = true;
	
		String link = String
		.format(
				"http://localhost:9000/secure/login");
		
		String message = String
		.format(
				"Dear %s,\nYour password is %s \nTo open your account, click on the following link: %s",
				 user.userName, user.password, link);
		
		
		Mail.send("coolSoft@SMT.com", user.email,
				"Forgot Password", message);
		}
		
		 renderJSON ( user ); 
		
	}
    public static void guestInvitation(String pId, String rId) {
		render("Users/guestInvitation.html", pId, rId);
	}
	
	/**
	 * This method renders the page of rejectInvitation
	 */
	public static void rejectInvitation() {
		String projectId = session.get("projectId");
		String roleId = session.get("roleId");
		render("Users/rejectInvitation.html", projectId, roleId);
	}
	

	/**
	 * This method renders the page of acceptInvitation and request to register
	 */
	public static void acceptInvitation() {
		String projectId = session.get("projectId");
		String roleId = session.get("roleId");
		render("Users/acceptInvitation.html", projectId, roleId);
	}

	private static long decryptValue(String someEncString)
	{
		return Long.parseLong(Crypto.decryptAES(someEncString));
	}
	public static void rejectInvitationEnc(@Required String pId, @Required String rId){
		long projectId = decryptValue(pId);
		long roleId = decryptValue(rId);
		session.put("projectId", projectId);
		session.put("roleId", roleId);
		rejectInvitation();
	}
	public static void acceptInvitationEnc(@Required String pId, @Required String rId){
		long projectId = decryptValue(pId);
		long roleId = decryptValue(rId);
		session.put("projectId", projectId);
		System.out.println(session.get("projectId"));
		session.put("roleId", roleId);
		Application.acceptInvitation();
	}
	
    

}